package com.xiaohong.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohong.goods.dao.SpuMapper;
import com.xiaohong.goods.pojo.Spu;
import com.xiaohong.goods.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpuServiceImpl implements SpuService {

    @Autowired
    private SpuMapper spuMapper;


    /**
     * 条件+分页查询Spu
     * @param spu 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public Page<Spu> findPage(Spu spu, int page, int size){
        // 构建查询条件
        QueryWrapper<Spu> queryWrapper = new QueryWrapper<Spu>(spu);
        // 构建分页
        Page pg = new Page(page, size);
        // 分页查询
        return spuMapper.selectPage(pg, queryWrapper);
    }

    /**
     * 分页查询Spu
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Spu> findPage(int page, int size){
        // 构建分页
        Page pg = new Page(page, size);
        // 分页查询
        return spuMapper.selectPage(pg, null);
    }

    /**
     * 多条件搜索方法Spu
     * @param spu
     * @return
     */
    @Override
    public List<Spu> findList(Spu spu){
        // 构建查询条件
        QueryWrapper<Spu> queryWrapper = new QueryWrapper<Spu>(spu);
        // 查询数据
        return spuMapper.selectList(queryWrapper);
    }

    /**
     * 根据ID删除Spu
     * @param id
     */
    @Override
    public void delete(String id){
        spuMapper.deleteById(id);
    }

    /**
     * 根据ID修改Spu
     * @param spu
     */
    @Override
    public void update(Spu spu){
        spuMapper.updateById(spu);
    }

    /**
     * 增加Spu
     * @param spu
     */
    @Override
    public void add(Spu spu){
        spuMapper.insert(spu);
    }

    /**
     * 根据ID查询Spu
     * @param id
     * @return
     */
    @Override
    public Spu findById(String id){
        return  spuMapper.selectById(id);
    }

    /**
     * 查询全部Spu数据
     * @return
     */
    @Override
    public List<Spu> findAll() {
        return spuMapper.selectList(null);
    }

    /**
     * 查询所有已审核并上架Spu
     * @return
     */
    @Override
    public List<Spu> findAllMarket() {
        // 构建查询条件
        QueryWrapper<Spu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_marketable",1);
        queryWrapper.eq("status",1);
        return spuMapper.selectList(queryWrapper);
    }

}
